#!/usr/bin/env python
import sys,os,queue,yaml,threading
base_dir = os.path.dirname(os.path.dirname(__file__))
sys.path.append(base_dir)
from bin.stools import s_tools
from moudle import cmd

q = queue.Queue()

def help():
    print('''
    state.sls:  Exec file of sls
        Param:[groupname] [filename]
        Eg: solt.py state.sls nginx vim-install
        ''')

class pkg(object):
    @classmethod
    def installed(cls,gname,args):
        for i in args:
            globals().update(i)
        q.put([gname,'yum -y install {}'.format(names)])

class file(object):
    @classmethod
    def managed(cls,gname,args):
        for i in args:
            globals().update(i)

        def exec_1():
            s_tools.process_mutl(gname,'ssh_put',base_dir + source,target)
        def exec_2():
            q.put([gname,'chown {}:{} {}'.format(user,group,target)])
            q.put([gname,'chmod {} {}' .format(mode,target)])
        t1 = threading.Thread(target=exec_1)
        t2 = threading.Thread(target=exec_2)
        t1.start()
        if t1.isAlive:
            t2.start()
            t2.join()


def sls(gname,slsfile):
    f = open(base_dir + '/sls/' + slsfile,'r')
    lscont = yaml.load(f)

    for pname in lscont.keys():
        for iname in lscont[pname].keys():
            eval(iname)(gname,lscont[pname][iname])

    for i in range(0, q.qsize()):
        intru = q.get()
        cmd.run(intru[0],intru[1])





